package com.example.androidkotlinmvvm.xml

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.androidkotlinmvvm.R
import com.example.androidkotlinmvvm.databinding.ItemMyBinding
import com.example.androidkotlinmvvm.mvvm.User2

class MyAdapter(private val items: MutableList<User2>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        //使用databing
        val binding: ItemMyBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.item_my,
            parent,
            false
        )
        return ViewHolder(binding)
        //不实用databing
        //        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_my, parent, false)
//        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//        holder.textView.text = items[position].name
        holder.binding.user=items[position]
    }

    override fun getItemCount(): Int {
        return items.size
    }

//    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
//        val textView: TextView = itemView.findViewById(R.id.tv)
//    }

    fun setData(myList: MutableList<User2>){
        this.items.clear()
        this.items.addAll(myList)
    }

    inner class ViewHolder(val binding: ItemMyBinding) :
        RecyclerView.ViewHolder(binding.root)
}